
const { ccclass, property } = cc._decorator;

@ccclass
export default class Loading extends cc.Component {

    @property(cc.Node) mask: cc.Node = null
    @property(cc.Node) anim: cc.Node = null

    private _count: number = 0

    public show() {
        this._count++
        if (this.node.active) return
        this.node.active = true
        this.mask.opacity = 0
        this.anim.opacity = 0
        cc.Tween.stopAllByTarget(this.node)
        cc.tween(this.node).delay(2).call(() => {
            this.playAnim()
        }).start()
    }

    public hide() {
        this._count--
        if (this._count <= 0) {
            this.node.active = false
            this._count = 0
            cc.Tween.stopAllByTarget(this.node)
        }
    }

    public forceHide() {
        cc.Tween.stopAllByTarget(this.node)
        this.node.active = false
        this._count = 0
    }

    private playAnim() {
        this.mask.opacity = 100
        this.anim.opacity = 255
    }

}
